
package flc.ast.dialog;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.CollectionUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lxj.xpopup.core.AttachPopupView;

import flc.ast.R;
import flc.ast.adapter.RateAdapter;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.bean.StkExchangeRateBean;
import stark.common.bean.StkExchangeRateList;

public class RateDialog extends AttachPopupView implements View.OnClickListener {

    private RecyclerView recyclerView;
    private RateAdapter rateAdapter;

    public RateDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_rate_style;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        initView();
    }

    protected void initView() {
        recyclerView = findViewById(R.id.rvRateList);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        rateAdapter = new RateAdapter();
        recyclerView.setAdapter(rateAdapter);
        rateAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (listener != null) {
                    listener.click(rateAdapter.getItem(position));
                }
            }
        });

        StkResApi.getMoneyRateList(this, new IReqRetCallback<StkExchangeRateList>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable StkExchangeRateList result) {
                if (success) {
                    if (!CollectionUtils.isEmpty(result.rateList)) {
                        rateAdapter.setList(result.rateList);
                    }
                } else {
                    ToastUtils.showShort(R.string.query_failure_tips);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
        }
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void click(StkExchangeRateBean bean);
    }

}
